package com.winchain.chainbill.bill.biz.receive.cmd;

import com.shell.bee.acct.AcctException;
import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.acct.account.po.AcctBkp;
import com.shell.bee.acct.transaction.po.AcctTxRec;
import com.shell.bee.acct.transaction.service.AcctTxRecService;
import com.shell.bee.base.utils.SpringContextUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.constant.rel.AssetCode;
import com.shell.framework.command.CommandContext;
import com.shell.framework.utils.SysDateUtil;
import com.winchain.chainbill.bill.base.po.CbRcv;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.biz.Bill;
import com.winchain.chainbill.bill.biz.send.cmd.TxCmd;
import com.winchain.chainbill.trade.service.BillAcctBlcService;
import com.winchain.chainbill.trade.service.TradeService;
import java.math.BigDecimal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillRcvRjctCrdtCmd extends TxCmd<CbRcv> {
	@Autowired
	private TradeService tradeService;
	@Autowired
	private AcctTxRecService AcctTxRecService;
	@Autowired
	private BillAcctBlcService billAcctBlcService;

	public boolean execute(CommandContext<Bill<CbRcv>> context) throws Exception {
		boolean timeout = ((Boolean) context.getValue("Timeout")).booleanValue();
		if (!timeout) {
			AcctTxRec tran = getAcctTxRec(context);
			CbRcv cbRcv = (CbRcv) ((Bill) context.getSource()).getSource();
			String txType = cbRcv.getTxType();
			if ("0".equals(txType)) {
				this.AcctTxRecService.save(tran);
				context.setValue("acctTxRec", tran);
			} else {
				tran.setAcctin(false);
				this.tradeService.callback(tran);
			}
		}
		return false;
	}

	private AcctTxRec getAcctTxRec(CommandContext<Bill<CbRcv>> context) {
		CbRec record = ((Bill) context.getSource()).getRec();
		CbRcv cbRcv = (CbRcv) ((Bill) context.getSource()).getSource();
		String billId = cbRcv.getRcvId();
		String buyerId = cbRcv.getBuyerId();

		BigDecimal billAmt = record.getBillAmt();
		String txType = cbRcv.getTxType();

		Object bizType = context.getValue("bizType");
		if (bizType == null) {
			throw new AcctException("������������������");
		}
		String abs = "";
		String creditAcctUse = "";
		String assetCode = "";
		String optType = "qt_ubk";
		String txChnCode = "";
		String finId = null;
		if ("0".equals(txType)) {
			abs = "����������-����";
			optType = "qt_res";
			creditAcctUse = "2";
			assetCode = AssetCode.XQT.name();
			txChnCode = "C";
			finId = record.getConfirmerId();
		} else if ("1".equals(txType)) {
			abs = "����������-����";
			creditAcctUse = "1";
			assetCode = AssetCode.XCB.name();
			txChnCode = "C";
		}
		AcctTxRec txRec = new AcctTxRec();
		txRec.setTxId(SystemUtil.getUUID());
		txRec.setTxSn(txRec.getTxId());

		txRec.setCurrCode(assetCode);
		txRec.setTxChnCode(txChnCode);

		AcctBas creditAcct = null;
		AcctBas cpAcct = null;
		AcctBkp txAcctBkp = null;
		if ("0".equals(txType)) {
			creditAcct = getAcctBas(finId, creditAcctUse, null);
			txAcctBkp = this.billAcctBlcService.getAcctBkp(creditAcct.getAcctNo(), txRec.getCurrCode(), null, null);

			cpAcct = getAcctBas(buyerId, "1", null);
		} else if ("1".equals(txType)) {
			creditAcct = getAcctBas(buyerId, creditAcctUse, null);
			txAcctBkp = this.billAcctBlcService.getAcctBkp(creditAcct.getAcctNo(), txRec.getCurrCode(), null, null);
		}
		txRec.setTxAcctNo(creditAcct.getAcctNo());
		txRec.setTxAcctNm(creditAcct.getAcctNm());
		txRec.setTxBankNm(creditAcct.getFiNm());

		txRec.setTxBkpId(txAcctBkp.getBkpId());

		txRec.setTxAmt(billAmt);
		txRec.setTxDt(SysDateUtil.getSystemDate());
		txRec.setTxTs(SysDateUtil.getSystemDateTime());
		txRec.setTxAbs(abs);
		txRec.setTxType((String) bizType);
		txRec.setOptType(optType);

		txRec.setTxSts("I");
		if (cpAcct != null) {
			txRec.setCpAcctNo(cpAcct.getAcctNo());
			txRec.setCpAcctNm(cpAcct.getAcctNm());
			txRec.setCpBankNm(cpAcct.getFiNm());
			AcctBkp cpAcctBkp = this.billAcctBlcService.getCreditAcctBkpByCont(finId, buyerId);

			txRec.setCpBkpId(cpAcctBkp.getBkpId());
		}
		txRec.setTxCashFlag("N");
		txRec.setTxReqFiCls("1");

		String sourceCode = SpringContextUtil.getProperty("system.code");
		txRec.setSrcCode(sourceCode);
		txRec.setBizId(billId);
		txRec.setDataScLvl("B");
		return txRec;
	}
}
